﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class MyEventObject : DontDestroyObject<MyEventObject>
{
    HashSet<Action> drawGizmos = new HashSet<Action>();
    HashSet<Action> updates = new HashSet<Action>();
    Action[] tempUpdates;
    HashSet<Action> lateUpdates = new HashSet<Action>();
    HashSet<Action> appQuits = new HashSet<Action>();
    HashSet<Action> onGUIs = new HashSet<Action>();
    Action[] tempOnGUIs;
//    public Action onEscape = null;
    bool updating;

    void OnDrawGizmos()
    {
        HashSet<Action>.Enumerator enumerator = drawGizmos.GetEnumerator();
        while (enumerator.MoveNext())
        {
            enumerator.Current();
        }
    }

    public void RegisterOnGUI(Action a)
    {
        onGUIs.Add(a);
    }

    public void UnregisterOnGUI(Action a)
    {
        onGUIs.Remove(a);
    }

    public void RegisterDrawGizmos(Action a)
    {
        drawGizmos.Add(a);
    }

    public void UnregisterDrawGizmos(Action a)
    {
        drawGizmos.Remove(a);
    }

    public void RegisterUpdate(Action a)
    {
        updates.Add(a);
    }

    public void UnregisterUpdate(Action a)
    {
        updates.Remove(a);
    }

    void Update()
    {
//        if (onEscape != null)
//        {
//            if (Input.GetKey(KeyCode.Escape))
//            {
//                onEscape();
//            }
//        }
        
        if (updates.Count <= 0)
        {
            return;
        }

        if (tempUpdates == null || tempUpdates.Length < updates.Count)
        {
            tempUpdates = new Action[updates.Count];
        }

        int count = updates.Count;
        updates.CopyTo(tempUpdates);

        for (int i = 0; i < tempUpdates.Length; i++)
        {
            if (i < count)
            {
                tempUpdates[i]();
            }
            else
            {
                tempUpdates[i] = null;
            }
        }
    }

    public void RegisterLateUpdate(Action a)
    {
        lateUpdates.Add(a);
    }

    public void UnregisterLateUpdate(Action a)
    {
        lateUpdates.Remove(a);
    }

    void LateUpdate()
    {
        HashSet<Action>.Enumerator enumerator = lateUpdates.GetEnumerator();
        while (enumerator.MoveNext())
        {
            enumerator.Current();
        }
    }

    public void RegisterappAppQuit(Action a)
    {
        appQuits.Add(a);
    }

    public void UnregisterAppQuit(Action a)
    {
        appQuits.Remove(a);
    }

	private void OnApplicationQuit()
	{
        HashSet<Action>.Enumerator enumerator = appQuits.GetEnumerator();
        while (enumerator.MoveNext())
        {
            enumerator.Current();
        }
	}

    protected override void myOnGUI()
    {
        if (onGUIs.Count <= 0)
        {
            return;
        }

        if (tempOnGUIs == null || tempOnGUIs.Length < onGUIs.Count)
        {
            tempOnGUIs = new Action[onGUIs.Count];
        }

        int count = onGUIs.Count;
        onGUIs.CopyTo(tempOnGUIs);

        for (int i = 0; i < tempOnGUIs.Length; i++)
        {
            if (i < count)
            {
                tempOnGUIs[i]();
            }
            else
            {
                tempOnGUIs[i] = null;
            }
        }
    }
}
